package model.trader;

import java.util.List;

import model.market.MarketInformation;
import model.market.MarketManager;
import model.trader.portfolio.EvolvingPortfolioManager;
import model.trader.portfolio.PortfolioManagementStrategy;
import model.trader.trade.TradeExecutionStrategy;

//AMDO: momentum trader shouldnt be balancing portfolio under constrained capital.. set him up more simply.

public class MomentumTrader extends BaseTrader {

	private static final double MISPRICE_LIMIT = 1.4;
	// private double requiredReturn = MarketManager.shareCashflowRate;// 1%

	private int noOfDaysRequiredForValueTest = 1;
	private int noOfDaysRequiredForMomentumTest = 3;

	public MomentumTrader() {
	}

	public MomentumTrader(int noOfDaysRequiredForValueTest, int noOfDaysRequiredForMomentumTest) {
		this.noOfDaysRequiredForValueTest = noOfDaysRequiredForValueTest;
		this.noOfDaysRequiredForMomentumTest = noOfDaysRequiredForMomentumTest;
	}

	public int getNoOfDaysRequiredForValueTest() {
		return noOfDaysRequiredForValueTest;
	}

	public void setNoOfDaysRequiredForValueTest(int i) {
		noOfDaysRequiredForValueTest = i;
	}

	// AM: shitty code.
	private int startPeriod;

	public int getStartPeriod() {
		return startPeriod;
	}

	public void setStartPeriod(int period) {
		this.startPeriod = period;
	}

	PortfolioManagementStrategy portfolioManager = new EvolvingPortfolioManager();

	// new StandardPortfolioStrategy();
	public PortfolioManagementStrategy getPortfolioStrategy() {
		return portfolioManager;
	}

	@Override
	public double[] getPreferences(MarketManager market, MarketInformation marketInfo) {
		if (market.getPeriod() < noOfDaysRequiredForValueTest + 1
				|| market.getPeriod() < noOfDaysRequiredForMomentumTest + 1) {
			// we havent got enough information to trade on.
			return null;
		}
		double[] fairPrices = new double[marketInfo.getNumberOfAssets()];

		for (int assetCount = 0; assetCount < marketInfo.getNumberOfAssets(); assetCount++) {
			List<Double> prices = marketInfo.getAssetPrices()[assetCount];
			double divvyReturn = marketInfo.getAssetCashflows()[assetCount].get(marketInfo.getAssetCashflows()[assetCount].size()-1);

			
			double originalPrice=prices.get(prices.size() - noOfDaysRequiredForMomentumTest-1);
			double nowPrice =prices.get(prices.size()-1);
			double capitalReturn = (nowPrice-originalPrice)/originalPrice;
			
			double averageCapital = capitalReturn / noOfDaysRequiredForMomentumTest*(marketInfo.getMarketPrices()[assetCount]);

			double averageCash = averageCapital + divvyReturn;
			// AM: avoid negative valuations..
			if (averageCash < 0) {
				averageCash = 0;
			}
			
			double fairPrice = averageCash
					/ (marketInfo.getRiskFreeReturns().get(marketInfo.getRiskFreeReturns().size() - 1) + marketInfo
							.getEquityRiskPremium());
			if(fairPrice>MISPRICE_LIMIT*(marketInfo.getMarketPrices()[assetCount])){
				fairPrice=MISPRICE_LIMIT*(marketInfo.getMarketPrices()[assetCount]);
			}
			if(fairPrice<(marketInfo.getMarketPrices()[assetCount])/MISPRICE_LIMIT){
				fairPrice=(marketInfo.getMarketPrices()[assetCount])/MISPRICE_LIMIT;
			}
			
			
			fairPrices[assetCount] = fairPrice;
//			if(assetCount==0){
//				System.out.println("Fair price 0 "+fairPrice+"   "+marketInfo.getAssetCashflows()[assetCount].get(marketInfo.getAssetCashflows()[assetCount].size()-1));
//			}
		}
		return fairPrices;
	}

	@Override
	public TradeExecutionStrategy getTradeStrategy() {
		return null;
	}

}
